package character.point.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import character.point.model.CharacterPoint;

import configuration.Paths;
import data.DataElement;
import data.DataTools;
import data.DataElement.DataElementType;

/**
 * @deprecated
 * @author Richard
 *
 */
public class CharacterPointDatabase {

	private static CharacterPointDatabase instance;

	private HashMap<String, CharacterPoint> points;

	private CharacterPointDatabase() {
		points = new HashMap<String, CharacterPoint>();
		this.updateFromFiles();
	}

	public static CharacterPointDatabase getInstance() {
		if (instance == null) {
			instance = new CharacterPointDatabase();
		}
		return instance;
	}

	public void updateFromFiles() {
		try {
			File spritesDir = new File(Paths.characterDataPath + File.separator
					+ "character_points.rpg");
			FileInputStream fis = new FileInputStream(spritesDir);
			ObjectInputStream ois = new ObjectInputStream(fis);
			DataElement rootElement = (DataElement) ois.readObject();
			ArrayList<DataElement> pointElements = rootElement
					.getAllElementsOfName("Character Point");
			for (DataElement de : pointElements) {
				CharacterPoint cp = new CharacterPoint((String) de
						.getChildElementOfName("Name").getValue(), (Integer) de
						.getChildElementOfName("Default Value").getValue(),
						(Integer) de.getChildElementOfName("Min Value")
								.getValue(), (Integer) de
								.getChildElementOfName("Max Value").getValue());
				points.put(cp.getName(), cp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<String> getAvailablePoints() {
		return new ArrayList<String>(points.keySet());
	}

	public CharacterPoint getPoint(String name) {
		return points.get(name);
	}

	public boolean pointNameExists(String name) {
		return points.keySet().contains(name);
	}

	public void addNewPoint(CharacterPoint cp) {
		if (!this.pointNameExists(cp.getName())) {
			points.put(cp.getName(), cp);
		}
	}

	public void deletePoint(String name) {
		points.remove(name);
	}

	public void changePointName(String oldName, String newName) {
		CharacterPoint temp = points.get(oldName);
		temp.setName(newName);
		points.remove(oldName);
		points.put(newName, temp);
	}

	public void save() {
		DataElement root = new DataElement("Character Point Database",
				DataElementType.COMPLEX, null);
		for (String s : points.keySet()) {
			CharacterPoint point = points.get(s);
			DataElement pointElement = new DataElement("Character Point",
					DataElementType.COMPLEX, null);
			pointElement.addChildElement(new DataElement("Name",
					DataElementType.STRING, point.getName()));
			pointElement.addChildElement(new DataElement("Default Value",
					DataElementType.INT, point.getDefaultValue()));
			pointElement.addChildElement(new DataElement("Min Value",
					DataElementType.INT, point.getMin()));
			pointElement.addChildElement(new DataElement("Max Value",
					DataElementType.INT, point.getMax()));
			root.addChildElement(pointElement);
		}
		try {
			File spritesDir = new File(Paths.characterDataPath + File.separator
					+ "character_points.rpg");
			FileOutputStream fos = new FileOutputStream(spritesDir);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(root);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
